package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.edu.controller.query.TeacherQuery;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.fegin.OssEduFegin;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.atguigu.guli.service.result.R;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "讲师模块")  //api是对你标注的类上进行说明
@RequestMapping("/edu/teacher")

public class AdminTeacherController {
    @Autowired
    TeacherService teacherService;

    @ApiOperation("批量删除讲师业务")
    @DeleteMapping("/del")
    public R batchDel(@RequestBody List<String> ids){
        boolean b = teacherService.removeByIds(ids);
        if(b){
            return R.ok();
        }else{
            return R.error();

        }
    }


    @ApiOperation("查询所有讲师")  //ApiOperation是对方法进行说明
    @GetMapping("/list")  //查询所有
    public R queryAll(){
        List<Teacher> list = teacherService.list();

        return R.ok().data("list",list);
    }

    @Autowired
    OssEduFegin ossEduFegin;
    @ApiOperation("删除讲师业务")
    @DeleteMapping("/del/{id}")
    public R del(@ApiParam(name = "id", value = "用户id",defaultValue = "2")
                           @PathVariable String id){
        //查询教师
        Teacher teacher = teacherService.getById(id);
        boolean b = teacherService.removeById(id);
        if(b){
            //判断头像地址是否为空,不为空删除
            if(StringUtils.isEmpty(teacher.getAvatar())){
                ossEduFegin.del(teacher.getAvatar(), "active");
            }

            return R.ok();
        }else{
            return R.error();

        }
    }

    @ApiOperation("对讲师进行修改")
    @PutMapping("/update")
    public R update(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation("查询指定讲师")
    @GetMapping("/addById/{id}")
    public R addById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/add")
    public R add(@RequestBody Teacher teacher){
        boolean b = teacherService.save(teacher);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation("分页查询")
    @GetMapping("/page/{pageNum}/{pageSize}")
    public R page(@PathVariable("pageNum") Integer pageNum,
                  @PathVariable("pageSize") Integer pageSize,
                  TeacherQuery query){


        Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);

        teacherPage = teacherService.getPageByCondition(teacherPage,query);


        return R.ok().data("page", teacherPage);

    }




}
